package com.example.cloud.controller.business.medical;

import com.alibaba.fastjson.JSON;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.basic.Project;
import com.example.cloud.model.business.medical.TreatPlanCourseDetails;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.business.medical.TreatPlanCourseDetailsService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/2.
 */
@RestController
@RequestMapping("/treat/plan/course/details")
public class TreatPlanCourseDetailsController extends BaseController {

    @Autowired
    private TreatPlanCourseDetailsService treatPlanCourseProjectService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, TreatPlanCourseDetails.class);
    }

    @GetMapping("/findByCourseId/{courseId}")
    public Result findByCourseId(@PathVariable Long courseId) {
        return ResultUtil.success(treatPlanCourseProjectService.findByCourseId(courseId));
    }

    @PostMapping("/save")
    public void save(@RequestBody TreatPlanCourseDetails record) {
        treatPlanCourseProjectService.doSave(record);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody List<TreatPlanCourseDetails> records) {
        treatPlanCourseProjectService.batchInsert(records);
    }

    @PostMapping("/deleteById/{id}")
    public void deleteById(@PathVariable Long id) {
        treatPlanCourseProjectService.doDeleteById(id);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids) {
        treatPlanCourseProjectService.doDelete(ids);
    }

    @PostMapping("/execute")
    public Result execute(@RequestBody Map map) {
        Long id = Long.valueOf(map.get("id").toString());
        int status = Integer.parseInt(map.get("status").toString());
        String content = map.get("content").toString();
        TreatPlanCourseDetails details = treatPlanCourseProjectService.findOne(id);
        if(status == StateConstants.EXECUTE_STATUS_YES){
            // 已使用数量
            int execute = details.getExecute();
            // 总数量
            int quantity = details.getQuantity();
            if(execute >= quantity){
                return ResultUtil.fail("治疗已结束");
            }
        }
        treatPlanCourseProjectService.doExecute(details, status, content);
        return ResultUtil.success("执行成功");
    }

    @PostMapping("/stop/{id}")
    public void stop(@PathVariable Long id) {
        treatPlanCourseProjectService.doStop(id);
    }

    @PostMapping("/replace")
    public Result replace(@RequestBody Map map) {
        return ResultUtil.success(treatPlanCourseProjectService.doReplace(Long.valueOf(map.get("id").toString()), JSON.parseArray(map.get("datas").toString(), Project.class)));
    }
}
